package demon2;

import java.util.Arrays;

public class Heap {
    public int[]elem;
    public int useSize;
    public Heap(){
        elem=new int[10];
    }
    public void assignment(int[] array){
        for (int i = 0; i < array.length; i++) {
            elem[i]=array[i];
            useSize++;
        }
    }
    public void swap(int i,int j){
        int temp=elem[i];
        elem[i]=elem[j];
        elem[i]=temp;
    }
    public void createHeap(){
        for (int parent=(useSize-1-1)/2; parent >=0 ; parent--) {
            shifDown(parent,useSize);
        }
    }
    public void shifDown(int parent,int end){
        int child=2*parent+1;
        while(child<end){
            if(child+1<end&&elem[child]<elem[child+1]){
                child++;
            }
            if(elem[child]>elem[parent]){
                swap(parent,child);
                parent=child;
                child=2*parent+1;
            }else {
                break;
            }
        }
    }
}
